import Vue from "vue";

import Vuex from "vuex";

Vue.use(Vuex);

// actions，用于响应组件中的动作
const actions = {
  // context 参数是 缩简版的store
  jia(context, value) {
    console.log("actions jia called");
    context.commit("JIA", value);
  },
  jiaOdd(context, value) {
    console.log("actions jiaOdd called");
    if (context.state.sum % 2) {
      context.commit("JIAODD", value);
    }
  },
  jiaWait(context, value) {
    console.log("actions jiaWait called");
    setTimeout(() => {
      context.commit("JIAWAIT", value);
    }, 500);
  },
  jian(context, value) {
    console.log("actions jian called");
    context.commit("JIAN", value);
  },
};

// mutations，用于操作数据(state)
const mutations = {
  JIA(state, value) {
    console.log("mutations JIA called");
    state.sum += value;
  },
  JIAODD(context, value) {
    console.log("mutations JIAODD called");
    state.sum += value;
  },
  JIAWAIT(context, value) {
    console.log("mutations JIAWAIT called");
    state.sum += value;
  },
  JIAN(state, value) {
    console.log("mutations JIAN called");
    state.sum -= value;
  },
};

// state，用于存储数据(state)
const state = {
  sum: 0,
  school:"清华",
  subject:'计算机',
};

// 准备getters 用于将state中的数据进行加工
const getters = {
  bigSum(state) {
    return state.sum * 10
  }
}

// 创建 并导出store
export default new Vuex.Store({
  actions,
  mutations,
  state,
  getters,
});
